package com.pinyougou.search.listener;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.pinyougou.es.EsItem;
import com.pinyougou.pojo.Item;
import com.pinyougou.service.GoodsService;
import com.pinyougou.service.ItemSearchService;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
    public class ItemMessageListener implements MessageListenerConcurrently {

        @Reference(timeout = 30000)
        private GoodsService goodsService;
        @Reference(timeout = 30000)
        private ItemSearchService itemSearchService;

        @Override
        public ConsumeConcurrentlyStatus
        consumeMessage(List<MessageExt> list,
                       ConsumeConcurrentlyContext consumeConcurrentlyContext) {
            System.out.println("===ItemMessageListener===");
            try {
                // 获取消息封装对象
                MessageExt messageExt = list.get(0);
                // 获取消息内容
                String content = new String(messageExt.getBody(),"UTF-8");
                // 把json字符串转化成List集合
                List<Long> ids = JSON.parseArray(content, Long.class);
                System.out.println("ids:" + ids);
                System.out.println("tags:" + messageExt.getTags());
                // 同步更新商品索引数据
                if ("UPDATE".equals(messageExt.getTags())) {
                    // 查询上架的SKU商品数据
                    List<Item> itemList = goodsService.findItemByGoodsId(ids);
                    // 判断集合
                    if (itemList.size() > 0) {
                        // 把List<Item>转化成List<SolrItem>
                        List<EsItem> esItems = new ArrayList<>();
                        for (Item item1 : itemList) {
                            EsItem esItem = new EsItem();
                            esItem.setId(item1.getId());
                            esItem.setTitle(item1.getTitle());
                            esItem.setPrice(item1.getPrice().doubleValue());
                            esItem.setImage(item1.getImage());
                            esItem.setGoodsId(item1.getGoodsId());
                            esItem.setCategory(item1.getCategory());
                            esItem.setBrand(item1.getBrand());
                            esItem.setSeller(item1.getSeller());
                            esItem.setUpdateTime(item1.getUpdateTime());
                            // 把SKU的规格json字符串，转化成Map集合(fastjson)
                            Map<String, String> spec = JSON
                                    .parseObject(item1.getSpec(), Map.class);
                            // 规格嵌套Field
                            esItem.setSpec(spec);
                            // 添加到集合
                            esItems.add(esItem);
                        }
                        // 把SKU商品数据同步到索引库
                        itemSearchService.saveOrUpdate(esItems);
                    }
                }
                // 删除商品索引数据
                if ("DELETE".equals(messageExt.getTags())) {
                   itemSearchService.delete(ids);
                }
            }catch (Exception ex){
                // 消费失败
                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
            // 消费成功
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    }


